package com.atguigu.flink.chapter10.query;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2023/6/29 09:09
 */
public class Query1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        //        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        
//        tEnv.sqlQuery("select a, b from (VALUES (1, 2.0), (2, 3.1)) AS t (a, b)").execute().print();
    
        tEnv.executeSql("create table sensor(" +
                            " id string, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv' " +
                            ")");
        
        //tEnv.sqlQuery("select distinct id from sensor").execute().print();
    
        /*tEnv.sqlQuery("select id, ts, sum(vc) vc_sum " +
                          " from sensor " +
                          " group by grouping sets(" +
                          " (id, ts), " +
                          " (id)," +
                          " (ts), " +
                          " ( ) " +
                          ")")
            .execute()
            .print();*/
       /* tEnv.sqlQuery("select id, ts, sum(vc) vc_sum " +
                          " from sensor " +
                          " group by rollup(" +  // 等价于:  (id, ts)  (id)  ()
                          " id, ts " +
                          ")")
            .execute()
            .print();*/
    
        tEnv.sqlQuery("select id, ts, sum(vc) vc_sum " +
                          " from sensor " +
                          " group by cube(" +  // 等价于:  (id, ts)  (id) (ts) ()
                          " id, ts" +
                          ")")
            .execute()
            .print();
        
        
    }
}
/*
上钻:


维度:


a b c
select
    ...
from t
group by a

select
    ...
from t
group by a b

select
    ...
from t
group by a b c

...



 */